package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.WorkOvertimeDTO;
import com.eastfair.venueservice.dto.WorkOvertimePageQuery;
import com.eastfair.venueservice.dto.WorkOvertimeSaveDTO;
import com.eastfair.venueservice.dto.WorkOvertimeUpdateDTO;
import com.eastfair.venueservice.entity.WorkOvertime;
import com.eastfair.venueservice.service.WorkOvertimeService;
import com.eastfair.venueservice.vo.WorkOvertimeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 加班申请
 * </p>
 *
 * @author dq
 * @date 2022-10-19
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/workOvertime")
@Api(value = "WorkOvertime", tags = "加班申请")
@PreAuth(replace = "venueservice:workOvertime:")
public class WorkOvertimeController extends SuperController<WorkOvertimeService, Long, WorkOvertime, WorkOvertimePageQuery, WorkOvertimeSaveDTO, WorkOvertimeUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private WorkOvertimeService workOvertimeService;

    @Override
    public void handlerResult(IPage<WorkOvertime> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<WorkOvertime> workOvertimeList = list
                .stream()
                .map((map) -> WorkOvertime.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(workOvertimeList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<WorkOvertimePageQuery> params) {
        WorkOvertimePageQuery pageQuery = params.getModel();
    }

    /**
     * 分页查询加班申请列表VO.
     *
     * @param pageParams the page params
     * @return the r
     */
    @ApiOperation("分页查询加班申请列表VO")
    @PostMapping("/query/workOvertime")
    public R<Page<WorkOvertimeVO>> queryWorkOvertime(@RequestBody PageParams<WorkOvertimeDTO> pageParams) {
        log.info("queryWorkOvertime - 分页查询加班申请列表VO, pageParams={}", pageParams);
        try {
            return R.successDef(workOvertimeService.listForVenue(pageParams));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 分页查询加班申请列表VO.
     *
     * @param query the page params
     * @return the r
     */
    @ApiOperation("分页查询加班申请列表VO")
    @PostMapping("/query/workOvertime/list")
    public R<List<WorkOvertimeVO>> queryWorkOvertime(@RequestBody WorkOvertimeDTO query) {
        log.info("queryWorkOvertime - 分页查询加班申请列表VO, query={}", query);
        try {
            return R.successDef(workOvertimeService.listWorkOvertimeVO(query));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 主场搭建商查询加班申请列表.
     *
     * @param pageParams the page params
     * @return the r
     */
    @PostMapping("/query/home/builder")
    public R<Page<WorkOvertimeVO>> queryForHome(@RequestBody PageParams<WorkOvertimeDTO> pageParams) {
        log.info("queryForHome - 主场搭建商查询加班申请列表, pageParams={}", pageParams);
        return R.successDef(workOvertimeService.listForHomeBuilder(pageParams));
    }

    /**
     * 加班单号获取加班详情VO.
     *
     * @param workOvertimeNumber the work overtime number
     * @return the by work overtime number
     */
    @ApiOperation("加班单号获取加班详情VO")
    @GetMapping("/get/{workOvertimeNumber}")
    public R<WorkOvertimeVO> getByWorkOvertimeNumber(@PathVariable("workOvertimeNumber") String workOvertimeNumber) {
        log.info("getByWorkOvertimeNumber - 加班单号获取加班详情VO, workOvertimeNumber={}", workOvertimeNumber);
        try {
            return R.successDef(workOvertimeService.getVOByWorkOvertimeNumber(workOvertimeNumber));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 订单号号获取加班详情VO.
     *
     * @param orderNumber the order number
     * @return the by order number
     */
    @ApiOperation("订单号号获取加班详情VO")
    @GetMapping("/get/order/{orderNumber}")
    public R<WorkOvertimeVO> getByOrderNumber(@PathVariable("orderNumber") String orderNumber) {
        log.info("getByOrderNumber - 订单号号获取加班详情VO, orderNumber={}", orderNumber);
        try {
            return R.successDef(workOvertimeService.getVOByOrderNumber(orderNumber));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 保存加班申请.
     *
     * @param workOvertimeDTO the work overtime dto
     * @return the r
     */
    @ApiOperation("保存加班申请")
    @SysLog("添加加班申请")
    @PostMapping("/save")
    public R<WorkOvertime> saveWorkOvertime(@RequestBody WorkOvertimeDTO workOvertimeDTO) {
        log.info("saveWorkOvertime - 保存加班申请, workOvertimeDTO={}", workOvertimeDTO);
        try {
            return R.successDef(workOvertimeService.saveWorkOvertime(workOvertimeDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 更新加班申请.
     *
     * @param workOvertimeDTO the work overtime dto
     * @return the r
     */
    @ApiOperation("更新加班申请")
    @SysLog("修改加班申请")
    @PostMapping("/update")
    public R<WorkOvertime> updateWorkOvertime(@RequestBody WorkOvertimeDTO workOvertimeDTO) {
        log.info("updateWorkOvertime - 更新加班申请, workOvertimeDTO={}", workOvertimeDTO);
        try {
            return R.successDef(workOvertimeService.updateWorkOvertime(workOvertimeDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 主场提交加班申请.
     *
     * @param workOvertimeDTO the work overtime dto
     * @return the r
     */
    @ApiOperation("提交加班申请")
    @SysLog("主场提交加班申请")
    @PostMapping("/submit/home")
    public R<Boolean> submitForHome(@RequestBody WorkOvertimeDTO workOvertimeDTO) {
        log.info("submitForHome - 主场提交加班申请, workOvertimeDTO={}", workOvertimeDTO);
        try {
            return R.successDef(workOvertimeService.submitForHome(workOvertimeDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 搭建商提交加班申请.
     *
     * @param workOvertimeDTO the work overtime dto
     * @return the r
     */
    @SysLog("搭建商提交加班申请")
    @PostMapping("/submit/builder")
    public R<Boolean> submitForBuilder(@RequestBody WorkOvertimeDTO workOvertimeDTO) {
        log.info("submitForBuilder - 搭建商提交加班申请, workOvertimeDTO={}", workOvertimeDTO);
        try {
            return R.successDef(workOvertimeService.submitForBuilder(workOvertimeDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 主场取消申请.
     *
     * @param workOvertimeNumber the work overtime number
     * @return the r
     */
    @ApiOperation("主场取消加班申请")
    @SysLog("主场取消加班申请")
    @GetMapping("/cancel/home/{workOvertimeNumber}")
    public R<Boolean> cancelHomeWorkOvertime(@PathVariable("workOvertimeNumber") String workOvertimeNumber) {
        log.info("cancelWorkOvertime - 主场取消加班申请, workOvertimeNumber={}", workOvertimeNumber);
        try {
            return R.successDef(workOvertimeService.cancelHomeWorkOvertime(workOvertimeNumber));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 搭建商取消加班申请.
     *
     * @param workOvertimeNumber the work overtime number
     * @return the r
     */
    @SysLog("搭建商取消加班申请")
    @GetMapping("/cancel/builder/{workOvertimeNumber}")
    public R<Boolean> cancelBuilderWorkOvertime(@PathVariable("workOvertimeNumber") String workOvertimeNumber) {
        log.info("cancelWorkOvertime - 搭建商取消加班申请, workOvertimeNumber={}", workOvertimeNumber);
        try {
            return R.successDef(workOvertimeService.cancelBuilderWorkOvertime(workOvertimeNumber));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 更新审核结果.
     *
     * @param workOvertimeDTO the work overtime dto
     * @return the r
     */
    @ApiOperation("更新审核结果")
    @SysLog("审核加班申请")
    @PostMapping("/audit")
    public R<Boolean> auditWorkOvertime(@RequestBody WorkOvertimeDTO workOvertimeDTO) {
        log.info("auditWorkOvertime - 更新审核结果, workOvertimeDTO={}", workOvertimeDTO);
        try {
            return R.successDef(workOvertimeService.auditBuilderWorkOvertime(workOvertimeDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 加班单号删除.
     *
     * @param workOvertimeNumber the ids
     * @return the r
     */
    @ApiOperation("加班单号删除")
    @SysLog("删除加班申请")
    @GetMapping("/remove/{workOvertimeNumber}")
    public R<Boolean> removeByWorkOvertimeNumber(@PathVariable("workOvertimeNumber") String workOvertimeNumber) {
        log.info("removeByWorkOvertimeNumber - 加班单号删除, workOvertimeNumber={}", workOvertimeNumber);
        try {
            return R.successDef(workOvertimeService.removeByWorkOvertimeNumber(workOvertimeNumber));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 更新支付结果.
     *
     * @param workOvertimeDTO the work overtime dto
     * @return the r
     */
    @ApiOperation("更新支付结果")
    @SysLog("支付加班申请")
    @PostMapping("/update/payStatus")
    public R<Boolean> updatePayStatus(@RequestBody WorkOvertimeDTO workOvertimeDTO) {
        log.info("updatePayStatus - 更新支付结果, workOvertimeDTO={}", workOvertimeDTO);
        try {
            return R.successDef(workOvertimeService.updateWorkOvertimePayStatus(workOvertimeDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }
}
